package graphicinterface;

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.LinkedList;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import bookworm.Book;
import bookworm.Review;

public class WindowShowReviewsFromBook implements MouseListener
{
	/**
	 * the respective book which all the reviews from the window will be related to
	 */
	private Book bookToShowReviews;
	
	/**
	 * each good rating review from the book will have a JPanel associated to it.
	 */
	private LinkedList<JPanel> panelsGoodRatingReviews;
	/**
	 * the same goes for the bad rating reviews
	 */
	private LinkedList<JPanel> panelsBadRatingReviews;
	
	
	private JFrame windowShowReviewsFromBook;
	public WindowShowReviewsFromBook(Book bookToShowReviews)
	{
		this.bookToShowReviews = bookToShowReviews;
		windowShowReviewsFromBook =
				new JFrameWithMenu("reviews from " + bookToShowReviews.getBookName());
		
		this.drawWindowShowReviewsFromBook();
	}
	
	/**
	 * given an address to a image of a mascot to be used in this window, it returns a
	 * ready JLabel with the image of the mascot.
	 * @param imageAddress address of the image where the mascot's picture is.
	 * @return a JLabel with the mascot picture.
	 */
	public JLabel getNewMascotCharacterToThisWindow(String imageAddress)
	{
		ImageIcon newMascot = CreatesImageIcon.createImageIcon(imageAddress, "mascot");
		JLabel labelForNewMascot = new JLabel(newMascot);
		return labelForNewMascot;
	}
	
	private void drawWindowShowReviewsFromBook()
	{
		//first, let's create the components of the window!
		JPanel panelBookWhereTheReviewsCameFrom = this.createPanelBookWhereReviewsCameFrom();
		//let's create a panel for good rating review
		JPanel panelGoodRatingReviews = this.createPanelGoodRatingReviews();
		/*why the "true" in the method above? because this method can create a panel for good or bad reviews. By this "true"
			I'm saying that I want the method to create a Jpanel for good rating reviews!
		*/
		JPanel panelBadRatingReviews = this.createPanelBadRatingReviews();
		
		JPanel panelGoodAndBadRatingReviews = new JPanel();
		panelGoodAndBadRatingReviews.setBackground(new Color(255,131,137));
		
		panelGoodAndBadRatingReviews.add(panelGoodRatingReviews);
		panelGoodAndBadRatingReviews.add(panelBadRatingReviews);
		
		//now, let's add these panels in one single JPanel!
		JPanelWithBackgroundImage panelShowsReviewsFromBook =  new JPanelWithBackgroundImage("backgroundbookworm.jpg");
		panelShowsReviewsFromBook.setBackground(new Color(255,131,137));
		GridBagLayout layoutShowsReviewsFromBook = new GridBagLayout();
		panelShowsReviewsFromBook.setLayout(layoutShowsReviewsFromBook);
		GridBagConstraints g = new GridBagConstraints();
		g.gridy = 0;
		g.gridx = 0;
		g.gridheight = 1;
		g.insets = new Insets(0, 0, 10, 0);
		panelShowsReviewsFromBook.add(panelBookWhereTheReviewsCameFrom, g);
		g.gridx = 0;
		g.gridy = 1;
		g.gridheight = 3;
		g.insets = new Insets(0, 0, 0, 0);
		panelShowsReviewsFromBook.add(panelGoodAndBadRatingReviews, g);
		
		
		this.windowShowReviewsFromBook.add(panelShowsReviewsFromBook);
		this.windowShowReviewsFromBook.setVisible(true);
	}
	
	private JPanel createPanelBookWhereReviewsCameFrom()
	{
		JPanel panelBook = new JPanel();
		panelBook.setBackground(new Color(255,131,137));
		String bookName = bookToShowReviews.getBookName();
		if(bookName.length() > 40)
		{
			//boo title is too long, we'll have to shorten it
			bookName = bookName.substring(0, 40) + "...";
		}
		JLabel labelBookTitle = TextLabelCreator.createStyledTextlabel("reviews from book:" + bookName);
		ImageIcon mediumCoverBook = GetsBookCoverByImageAddress.getBookCover(this.bookToShowReviews.getUrlToCover());
		//how do we display an ImageIcon in a Jpanel?By a Jlabel!
		JLabel labelCoverBook = new JLabel(mediumCoverBook);
		JLabel labelsInstructions = TextLabelCreator.createStyledTextlabel("press tab to alternate between choosing good or bad reviews", 15);
		
		//now let's join the book title and it's medium cover in panelBook!
		GridBagLayout layoutPanelBook = new GridBagLayout();
		panelBook.setLayout(layoutPanelBook);
		GridBagConstraints g = new GridBagConstraints();
		g.gridy = 0;
		panelBook.add(labelBookTitle, g);
		g.gridy = 1;
		panelBook.add(labelCoverBook, g);
		g.gridy = 2;
		panelBook.add(labelsInstructions, g);
		
		return panelBook;
	}
	
	
	private JPanel createPanelGoodRatingReviews()
	{
		
		//then let's get the good rating reviews.
		LinkedList<Review> goodRatingReviews = this.bookToShowReviews.getGoodRatingReviews();
		//each review will have a JPanel associated with it. Let's create those panels!
		this.panelsGoodRatingReviews = createPanelsGoodOrBadRatingReviews(goodRatingReviews);
		
		/*not all the good rating reviews will be displayed at the same time in the window:
		 * we'll only display 1 per time for more space. For this, we'll use the panel that 
		 * displays other panels with limit of elements per page: the ShowsJPanelsOnManyPages.
		 */
		ShowsJPanelsOnManyPages panelGoodRatingReviewsTitleAndCharacter = 
				new ShowsJPanelsOnManyPages(this.panelsGoodRatingReviews, 1, 1);
		panelGoodRatingReviewsTitleAndCharacter.setBackground(new Color(255,131,137));
		//now let's create a label for this panel which will show that these are the good rating reviews:
		
		JLabel labelGoodRating = TextLabelCreator.createStyledTextLabel("Good rating reviews", new Color(8, 243, 240));
		
		//finally let's join this label with the panel we just created
		JPanel panelGoodRatingReviews = new JPanel();
		panelGoodRatingReviews.setBackground(new Color(255,131,137));
		GridBagLayout layoutGoodRatingReviews = new GridBagLayout();
		panelGoodRatingReviews.setLayout(layoutGoodRatingReviews);
		GridBagConstraints g = new GridBagConstraints();
		g.gridy = 0;
		g.gridheight = 1;
		panelGoodRatingReviews.add(labelGoodRating, g);
		g.gridy = 1;
		g.gridheight = 3;
		panelGoodRatingReviews.add(panelGoodRatingReviewsTitleAndCharacter, g);
		
		return panelGoodRatingReviews;
	}
	
	private JPanel createPanelBadRatingReviews()
	{
		//let's get the bad rating reviews.
		LinkedList<Review> badRatingReviews = this.bookToShowReviews.getBadRatingReviews();
		//each review will have a JPanel associated with it. Let's create those panels!
		this.panelsBadRatingReviews = createPanelsGoodOrBadRatingReviews(badRatingReviews);
		
		/*not all the bad rating reviews will be displayed at the same time in the window:
		 * we'll only display 1 per time for more space. For this, we'll use the panel that 
		 * displays other panels with limit of elements per page: the ShowsJPanelsOnManyPages.
		 */
		ShowsJPanelsOnManyPages panelBadRatingReviewsTitleAndCharacter = 
				new ShowsJPanelsOnManyPages(this.panelsBadRatingReviews, 1, 1);
		panelBadRatingReviewsTitleAndCharacter.setBackground(new Color(255,131,137));
		//now let's create a label for this panel which will show that these are the bad rating reviews:
		
		JLabel labelBadRating = TextLabelCreator.createStyledTextLabel("Bad rating reviews", new Color(8, 243, 240));
		
		//finally let's join this label with the panel we just created
		JPanel panelBadRatingReviews = new JPanel();
		panelBadRatingReviews.setBackground(new Color(255,131,137));
		GridBagLayout layoutBadRatingReviews = new GridBagLayout();
		panelBadRatingReviews.setLayout(layoutBadRatingReviews);
		GridBagConstraints g = new GridBagConstraints();
		g.gridy = 0;
		g.gridheight = 1;
		panelBadRatingReviews.add(labelBadRating, g);
		g.gridy = 1;
		g.gridheight = 3;
		panelBadRatingReviews.add(panelBadRatingReviewsTitleAndCharacter, g);
		
		return panelBadRatingReviews;
	}

	/**
	 * given a list of good or bad rating reviews, it creates JPanels for them. The result
	 * will be set as one of the lists of JPanel that are attributes of this class.
	 * @param goodOrBadRatingReviews
	 */
	private LinkedList<JPanel> createPanelsGoodOrBadRatingReviews(LinkedList<Review> goodOrBadRatingReviews) 
	{
		LinkedList<JPanel> panelsGoodOrBadRatingReviews = new LinkedList<JPanel>();
		for(int i = 0; i < goodOrBadRatingReviews.size(); i++)
		{
			Review singlereview = goodOrBadRatingReviews.get(i);
			PanelSingleReview panelSingleReview = new PanelSingleReview(singlereview);
			//each panel of a single review can be selected via mouse. 
			panelSingleReview.addMouseListener(this);
			panelsGoodOrBadRatingReviews.add(panelSingleReview);
		}
		
		return panelsGoodOrBadRatingReviews;
	}

	@Override
	public void mouseClicked(MouseEvent arg0) 
	{
		/*
		 * when the user clicks on something from this interface, it's probably a single review
		 * panel because it's the only clickable thing in the window. A single review panel has already
		 * a mouseListener to open a new page with a single review. This leaves this window with only one 
		 * obligation: to close it's own frame. 
		 */
		this.windowShowReviewsFromBook.dispose();
		
	}
	@Override
	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mousePressed(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	
	
	

}
